class OrdersController < ApplicationController
  # GET /orders
  # GET /orders.xml
  before_filter :initialize_cart
  before_filter :login_required,:only=>[:show,:create,:new]
  before_filter :admin?,:only=>[:index,:destroy,:update,:edit]
  def index
    @orders = Order.paginate(:page=>params[:page],:order=>"id desc")
    respond_to do |format|
      format.html {render :layout=>"admin"}
      format.xml  { render :xml => @orders }
    end
  end

  def list_items
    @order=Order.find(params[:id])
    render :layout=>"admin"
  end

  # GET /orders/1
  # GET /orders/1.xml
  def show
    @order = Order.find(params[:id],:conditions=>["user_id=?",session[:user_id]])
    @webconfig = Webconfig.find_by_name("paymethod")
    @page_title = "订单详情"
    @page_nav="><a href='/procates/all/products/list'>产品中心</a>>&nbsp;#{@page_title}"
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @order }
    end
  end

  # GET /orders/new
  # GET /orders/new.xml
  def new
    @order = Order.new
    @user=User.find(session[:user_id])
    @order.email=@user.email
    flash.now[:notice] = '请详细填写以下表单，以确定是否能够送货，谢谢。'
    @page_title = "结算中心"
    @page_nav="><a href='/procates/all/products/list'>产品中心</a>>&nbsp;#{@page_title}"
    if @cart.cart_items.empty?
      flash[:notice] = "您的购物车为空，请先选购产品."
      redirect_to :back
    end
   
  end

  # GET /orders/1/edit
  def edit
    @order = Order.find(params[:id])
    render :layout=>"admin"
  end

  # POST /orders
  # POST /orders.xml
  def create
    @order = Order.new(params[:order])
    @order.user_id=session[:user_id]
    @order.status="订单提交"
    @cart.cart_items.each do |item|
      @order.order_items << OrderItem.new(:product_id => item.product_id,:price => item.price,:amount => item.amount)
    end

    respond_to do |format|
      if @order.save
        flash[:notice] = '谢谢，订单提交成功！请按以下提供的支付方式进行支付，我们会尽快处理。'
        # Empty the cart
        @cart.cart_items.destroy_all
        format.html { redirect_to(@order) }
        format.xml  { render :xml => @order, :status => :created, :location => @order }
      else
        @page_title = "结算中心"
        @page_nav="><a href='/procates/all/products/list'>产品中心</a>>&nbsp;#{@page_title}"
        format.html { render :action => "new" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /orders/1
  # PUT /orders/1.xml
  def update
    @order = Order.find(params[:id])
    #@order.status=params[:order][:status]
    respond_to do |format|
      if @order.update_attribute("status", params[:order][:status])
        flash[:notice] = 'Order was successfully updated.'
        format.html {redirect_to(orders_path) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit",:layoutj=>"admin" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.xml
  def destroy
    @order = Order.find(params[:id])
    @order.destroy

    respond_to do |format|
      format.html { redirect_to(orders_url) }
      format.xml  { head :ok }
    end
  end
end
